<?php
namespace app\common\component\seeding;

use app\common\model\SeedingFirst as SeedingFirstModel;

class SeedingFirstService {
	public function create($data) {
		$seedingFirstModel = SeedingFirstModel::create($data);
		
		if($seedingFirstModel) {
			return [
				'code' => 0,
				'data' => ['seed_first' => $seedingFirstModel]
			];
		}

		return [
			'code' => -1,
			'message' => '创建失败,seed_first'
		];
	}

	public function getProfit($user_id) {
		$seedingModelSelect = SeedingFirstModel::where('status in(3, 4) AND user_id = ' . $user_id) -> order('seeding_first_id ASC') -> select();

		if(!empty($seedingModelSelect)) {
			$total = 0;

			foreach($seedingModelSelect as $seedingModel) {
				switch($seedingModel -> getData('status')) {
					case 3:
						$startDays = countDays($seedingModel -> start_time);
						$totalDays = countDays($seedingModel -> start_time, $seedingModel -> end_time);

						$SEED_FIRST_GROW_UP_PROFIT = getFarmParam('SEED_FIRST_GROW_UP_PROFIT') / 100;

						if($startDays >= $totalDays) {
							$seedingModel -> status = 4;

							$profit = $seedingModel -> price * $SEED_FIRST_GROW_UP_PROFIT * $totalDays;
						} else {
							$profit = $seedingModel -> price * $SEED_FIRST_GROW_UP_PROFIT * $startDays;
						}

						$profit = round($profit, 2);
						if($profit != $seedingModel -> profit) {
							$seedingModel -> profit = $profit;
							$seedingModel -> save();
						}
						break;
				}

				$total += $seedingModel -> price + $seedingModel -> profit;
			}

			return [
				'code' => 0,
				'data' => [
					'total' => $total
				]
			];
		}

		return [
			'code' => 0,
			'data' => [
				'total' => 0
			]
		];
	}
}